1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.NoSuchElementException;
22  
23  import javax.annotation.Nullable;
24  
25  /**
26   * This class provides a skeletal implementation of the {@code Iterator}
27   * interface for sequences whose next element can always be derived from the
28   * previous element. Null elements are not supported, nor is the
29   * {@link #remove()} method.
30   *
31   * <p>Example: <pre>   {@code
32   *
33   *   Iterator<Integer> powersOfTwo = 
34   *       new AbstractSequentialIterator<Integer>(1) {
35   *         protected Integer computeNext(Integer previous) {
36   *           return (previous == 1 << 30) ? null : previous * 2;
37   *         }
38   *       };}</pre>
39   *
40   * @author Chris Povirk
41   * @since 12.0 (in Guava as {@code AbstractLinkedIterator} since 8.0)
42   */
43  @GwtCompatible
44  public abstract class AbstractSequentialIterator<T>
45      extends UnmodifiableIterator<T> {
46    private T nextOrNull;
47  
48    /**
49     * Creates a new iterator with the given first element, or, if {@code
50     * firstOrNull} is null, creates a new empty iterator.
51     */
52    protected AbstractSequentialIterator(@Nullable T firstOrNull) {
53      this.nextOrNull = firstOrNull;
54    }
55  
56    /**
57     * Returns the element that follows {@code previous}, or returns {@code null}
58     * if no elements remain. This method is invoked during each call to
59     * {@link #next()} in order to compute the result of a <i>future</i> call to
60     * {@code next()}.
61     */
62    protected abstract T computeNext(T previous);
63  
64    @Override
65    public final boolean hasNext() {
66      return nextOrNull != null;
67    }
68  
69    @Override
70    public final T next() {
71      if (!hasNext()) {
72        throw new NoSuchElementException();
73      }
74      try {
75        return nextOrNull;
76      } finally {
77        nextOrNull = computeNext(nextOrNull);
78      }
79    }
80  }